package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.zcx.helper.secret.SecretAESDESede;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

import longcaisuyun.longcai.com.mystatic.publicUrl;
import longcaisuyun.longcai.com.utils.TimeCount;
import wh.utils.StringUtils;


/**
 *
 */
public class WangJiActivity extends Activity implements View.OnClickListener{

    Button bt_next,button7;
    LinearLayout  fanhui;
    EditText editText4,editText3;
    String code;
    TimeCount timeCount;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wang_ji);
        View view  = getWindow().getDecorView();
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        ViewInit();
        fanhui= (LinearLayout) findViewById(R.id.fanhui);
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void ViewInit() {
        bt_next= (Button) findViewById(R.id.bt_next);
        bt_next.setOnClickListener(this);
        button7= (Button) findViewById(R.id.button7);
        button7.setOnClickListener(this);
        editText3= (EditText) findViewById(R.id.editText3);
        editText4= (EditText) findViewById(R.id.editText4);
    }


    public void onClick(View v) {
        switch (v.getId()){
            case   R.id.bt_next:
                if (StringUtils.isEmpty(editText3.getText().toString())){
                Toast.makeText(WangJiActivity.this, "您还未输入手机号码", Toast.LENGTH_SHORT).show();
                }else if (!wh.utils.StringUtils.isPhone(editText3.getText().toString())){
                    Toast.makeText(WangJiActivity.this, "输入的手机号码格式错误，请检查后重新输入", Toast.LENGTH_SHORT).show();
                }else if (wh.utils.StringUtils.isEmpty(editText4.getText().toString().trim())){
                    Toast.makeText(WangJiActivity.this, "请输入验证码", Toast.LENGTH_SHORT).show();
                }else if (!editText4.getText().toString().trim().equals(code)){
                    Toast.makeText(WangJiActivity.this, "您输入的验证码不正确", Toast.LENGTH_SHORT).show();
                }else{
                    Intent intent=new Intent();
                    intent.setClass(WangJiActivity.this,SzmmActivity.class);
                    intent.putExtra("model", "1");
                    intent.putExtra("tel",editText3.getText().toString());
                    startActivity(intent);
                    this.finish();
                }
                break;
            case R.id.button7:
                if (StringUtils.isEmpty(editText3.getText().toString())){
                    Toast.makeText(WangJiActivity.this, "您还未输入手机号码", Toast.LENGTH_SHORT).show();
                }else if (!wh.utils.StringUtils.isPhone(editText3.getText().toString())){
                    Toast.makeText(WangJiActivity.this, "输入的手机号码格式错误，请检查后重新输入", Toast.LENGTH_SHORT).show();
                }else{
                  httpPost(editText3.getText().toString(), "2");
                }
                break;
        }
    }
    //    验证码接口
    private void httpPost(String mobile, String action) {
        final SecretAESDESede secretAESDESede = new SecretAESDESede("123456781234567812345678", "12345678", SecretAESDESede.DESEDE_CBC_PKCS5PADDING);
        final FinalHttp finalHttp = new FinalHttp();
        AjaxParams params = new AjaxParams();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mobile", mobile);
            jsonObject.put("action", action);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            params.put("Keys", secretAESDESede.encrypt(jsonObject.toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }


        finalHttp.post(publicUrl.mobilecode, params, new AjaxCallBack<String>() {


            public void onStart() {
                super.onStart();
                MyApplication.myviewutil.startProgressDialog(WangJiActivity.this, finalHttp, publicUrl.mobilecode);
            }

            public void onSuccess(String s) {
                super.onSuccess(s);

                try {
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(secretAESDESede.decrypt(s));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Log.e("什么鬼", jsonObject.toString());
                    String Message = jsonObject.getString("message");
                    if (Message.equals("1") ) {
                        timeCount = new TimeCount(60000, 1000, button7);
                        timeCount.start();
                        code = jsonObject.getString("code");
                        Toast.makeText(WangJiActivity.this, "验证码发送成功", Toast.LENGTH_SHORT).show();
                    }
                    if (Message.equals("2")){
                        Toast.makeText(WangJiActivity.this, "手机号码不存在，请重新输入", Toast.LENGTH_SHORT).show();
                    }
                    if (Message.equals("0")) {
                        Toast.makeText(WangJiActivity.this, "获取验证码失败，请重新获取", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                MyApplication.myviewutil.stopProgressDialog();
            }

            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                MyApplication.myviewutil.stopProgressDialog();
                Toast.makeText(WangJiActivity.this, "您的网络不好,请稍后重试", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
